<?php
namespace app\admin\controller;
//导入model
use  \app\admin\model\Pictures;
use think\Controller;
use \app\admin\AdminCommon;
use think\validate;
use think\Db;
class Picture extends AdminCommon
{
    public function getIndex()
    {
        $info = Pictures::all();
        //把数据分配到模板
        $this->assign('data', $info);
        //加载模板传入编号
        return $this->fetch("picture/index", ['i' => 1]);
    }

    //添加
    public function getAdd()
    {
        return $this->fetch("picture/add");

    }

    //执行添加
    public function postDoadd()
    {
        //判断数据库
        if (count(Db::table("tp_picture")->select()) <= 5) {
            //创建请求
            $request = request();
            $date = $request->param();
            //删除多余的字段
            unset($date['pic']);
            unset($date['action']);
            unset($date['file']);
            //判断时间
            if ($date['create_time']) {
                $create_time = strtotime($date['create_time']);
                $date['create_time'] = $create_time;
            }
            //判断结束时间
            if ($date['update_time']) {
                $update_time = strtotime($date['update_time']);
                $date['update_time'] = $update_time;
            }
            $info = Db::table("tp_picture")->insert($date);
            if ($info) {
                $data = [
                    'status' => 1,
                    'msg' => '添加成功',
                ];
            } else {
                $data = [
                    'status' => 0,
                    'msg' => '添加失败',
                ];
            };
            return json($data);
        } else {
            $this->error('数据超过了', "picture/index");
            return false;
        }
    }

    //图片上传
    public function postpics()
    {
        $request = request();
        $files = $request->file('file');
        // 移动到框架应用根目录/public/uploads/ 目录下
        if ($files) {
            //加条件
            $info = $files->validate(['size' => 3146667778888, 'ext' => 'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/picture_pic');
            if ($info) {
                // 成功上传后 获取上传信息
                $pic = $info->getSaveName();
                return json(array('status' => 1, 'path' => $pic, 'msg' => '图片上传成功'));
            } else {
                // 上传失败获取错误信息
                echo $info->getError();
                return json(array('status' => 0, 'msg' => '上传失败'));
            }
        }
    }

    //显示
    public function poststart($id)
    {
        $picture = new \app\admin\model\Pictures();
        $result = $picture->save(['picture_status' => '1'], ['id' => $id]);
        //print_r($result);exit;
        if ($result) {
            $data = [
                'status' => 1,
                'msg' => '已显示',
                'id' => $id,
            ];
        } else {
            $data = [
                'status' => 0,
                'msg' => '操作失败，请稍后重试',
                'id' => $id,
            ];
        }
        return json($data);

    }

    //禁用
    public function poststop($id)
    {
        $picture = new \app\admin\model\Pictures();
        $result = $picture->save(['picture_status' => '0'], ['id' => $id]);
        if ($result) {
            $data = [
                'status' => 1,
                'msg' => '已禁用',
                'id' => $id,
            ];
        } else {
            $data = [
                'status' => 0,
                'msg' => '操作失败，请稍后重试',
                'id' => $id,
            ];
        }
        return json($data);
    }

    //批量删除
    public function postdatadel($ids)
    {
        //拼接id得值
        $ids = explode(",", $ids);
        $paths = array();
        Db::startTrans();
        try {
            foreach ($ids as $id) {
                $picture = \app\admin\model\Pictures::get($id);
                //  根据id查询出图片的路径
                $path = $picture->where('id', $id)->value('picture_pic');
                //拼接路径
                $path = "./uploads/picture_pic/" . $path;
                //把获取到的数据插入到数组中
                array_push($paths, $path);
                $result = $picture->where('id', $id)->delete();
            }
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }
        if ($result) {
            foreach ($paths as $pathes) {
                //删除图片
                unlink($pathes);
                $pathe = pathinfo($pathes, PATHINFO_DIRNAME);
                if (count(scandir($pathe)) == 2) {
                    rmdir($pathe);
                }

            }
            $data = [
                'status' => 1,
                'msg' => '批量删除成功',
            ];
        } else {
            $data = [
                'status' => 0,
                'msg' => '批量删除失败',
            ];
        }
        return $data;
    }

    //编辑
    public function getedit($id)
    {
        $picture = \app\admin\model\Pictures::get($id);
        $this->assign('data', $picture);
        return $this->fetch("picture/edit");
    }

    //执行修改
    public function postdoedit()
    {
        $request = request();
        $res = $request->param();
        $old_pic = Db::table('tp_picture')->where('id', $res['id'])->find();
        $old = $old_pic['picture_pic'];
        unset($res['action']);
        unset($res['file']);
        if ($res['create_time']) {
            $create_time = strtotime($res['create_time']);
            $res['create_time'] = $create_time;
        }
        //判断结束时间
        if ($res['update_time']) {
            $update_time = strtotime($res['update_time']);
            $res['update_time'] = $update_time;
        }
        //没有上传新的图片
        if ($res['picture_pic'] == $old) {
            $result = Db::table('tp_picture')->where('id', $res['id'])->update($res);
            $data = [
                'status' => 1,
                'msg' => '更新成功',
            ];
            //上传了新图片
        } else {
            $path = "./uploads/picture_pic/".$old;
            unlink($path);
            $resl = Db::table('tp_picture')->where('id', $res['id'])->update($res);
            if ($resl) {
                $data = [
                    'status' => 1,
                    'msg' => '更新成功',
                ];
            } else {
                $data = [
                    'status' => 0,
                    'msg' => '更新失败',
                ];
            }
        }
        return json($data);
    }

    //删除
    public function postdel($id)
    {
        $picture = \app\admin\model\Pictures::get($id);
        //查询出图片路经
        $path = $picture->where('id', $id)->value('picture_pic');
        $paths = "./uploads/picture_pic/" . $path;
        if ($picture->delete()) {
            if (file_exists($paths) && is_file($paths)) {
                if (unlink($paths)) {
                    $pathe = pathinfo($paths, PATHINFO_DIRNAME);
                    if (count(scandir($pathe)) == 2) {
                        rmdir($pathe);
                    }
                }
                $data = [
                    'status' => 1,
                    'msg' => '批量删除成功',
                ];
            } else {
                $data = [
                    'status' => 0,
                    'msg' => '批量删除失败',
                ];
            }
            return $data;

        }
    }

}